<%--
  Created by IntelliJ IDEA.
  User: Joshua
  Date: 7/17/12
  Time: 6:15 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="beans.*" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

<html>
<head>
    <title>Squire: Game Selection</title>
    <link rel="STYLESHEET" type="text/css" href="css/squire.css" />

    <!--include external js files and stylesheets-->
    <script type="text/javascript" language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" src="scripts/charSelect.js"></script>
    <link rel="STYLESHEET" href="css/squire.css" type="text/css" />
</head>
<body>
<jsp:useBean id="user" class="beans.UserBean" scope="page"/>
<jsp:setProperty name="user" property="*" />

<%--set the datasource--%>
<jsp:include page="includes/header.html"></jsp:include>
<div class="wrapper">
    <table class="select">
        <tr>
            <th>Rulesets</th>
        </tr>
        <tr>
            <%--validate that the user exists--%>
            <c:catch var="queryExc">
                <sql:query var="loginResult" scope="page">
                    select 1
                    from users
                    where userid = ? and password = ?
                    <sql:param value="${user.userid}" />
                    <sql:param value="${user.password}" />
                </sql:query>
            </c:catch>
            <c:choose>
                <c:when test="${loginResult.rowCount == 0}">
                    <c:set var="failedLogin" value="true" scope="request" />
                    <jsp:forward page="index.jsp" />
                </c:when>
                <c:when test="${empty queryExc}">
                    <c:catch var="queryExc">
                        <%--The user login credentials passed: display the user's rulesets--%>
                        <sql:query var="rulesets" scope="page">
                            select name
                            from rulesets
                        </sql:query>
                    </c:catch>
                    <c:choose>
                        <c:when test="${rulesets.rowCount == 0}">
                            <td>I'm terribly sorry, but Squire could not locate
                            any rulesets for you to play with. Please try again
                            later or contact the system administrator.</td>
                        </c:when>
                        <c:when test="${empty queryExc}">
                            <c:forEach var="row" items="${rulesets.rowsByIndex}">
                                <c:forEach var="ruleset" items="${row}">
                                    <td><a href="/fetchCampaigns?userid=${user.userid}&ruleset=${ruleset}">${ruleset}</a></td>
                                </c:forEach>
                            </c:forEach>
                        </c:when>
                        <c:otherwise>
                            <td>The server threw an exception whilst looking for rulesets:
                            ${queryExc}</td>
                        </c:otherwise>
                    </c:choose>
                </c:when>
                <c:otherwise>
                    <td>The server threw an exception while trying to
                    verify login credentials.<br/>
                    ${queryExc}</td>
                </c:otherwise>
            </c:choose>
        </tr>
    </table>
</div>
</body>
</html>